/*
* Fastjs official plugin
*
* About this plugin:
*   Version: v1.0.0
*   Plugin: debounce.fs.js
*   Author: Fastjs Team
*   Contact-Us: xiaodong@indouyin.cn
*/

class debounce {
    constructor(ms, callback, pid) {
        if (typeof pid !== "number" && pid) {
            fastjs.throwCrashError("Class.debounce.__constructor__", "pid", "type");
            return void 0;
        }
        // Pid setup
        this.pid = !pid ? fastjs.random(10000000, 99999999) : pid;
        if (typeof ms !== "number" && ms) {
            fastjs.throwCrashError("Class.debounce.__constructor__", "ms", "type");
            return void 0;
        }
        this.ms = !ms ? 1000 : ms;
        if (!callback) {
            fastjs.qualityOutput("Class.debounce.__constructor__", "Callback is null, it will not do anything when trigger")
        }
        if (typeof callback !== "function" && callback) {
            if (typeof callback === "string") {
                fastjs.qualityOutput("Class.debounce.__constructor__", "We recommend you use function type to give a javascript")
            } else {
                fastjs.throwCrashError("Class.debounce.__constructor__", "callback", "type");
                return void 0;
            }

        }
        this.callback = !callback ? () => {
        } : callback;
        this.trigger = false;
        return this;
    }

    trig() {
        if (this.trigger) {
            clearTimeout(this.trigger);
        }
        this.trigger = setTimeout(() => {
            this.trigger = false;
            if (typeof this.callback === "function") {
                this.callback();
            } else {
                eval(this.callback);
            }
        }, this.ms);
        return this;
    }
}